﻿//============================================================
//Tree
//============================================================
function Tree() {
    var _this = this;
    _this.ID = ""; //树生成时的ID
    _this.TextClickType = "click"; //click/expand/
    _this.TextClickFun = null; //单机文本时响应的方法名称
    //_this.TextDbClickFun = null;
    _this.ReturnType = "value"; //value/text/text:value
    _this.SelectAllChild = true; //单机父节点时是否选中它所有的子节点
    //_this.SelectChild = false;
    //_this.CheckBoxClickFun = null;
    _this.ImgWidth = "16px"; //默认图片宽度
    _this.ImgHeight = "16px"; //默认图片高度
    _this.SetNodeTextAsNodeTitle = true;
    _this.Content = "";

    var MaxWidth = 0;

    _this.initTree = function (itemList, parentID) {
        window.status = "";
        InitTreeParent(itemList, parentID);

        var expendedNode = getPageSessionStorage(_this.ID + "Expended");
        if (expendedNode && expendedNode != null) {
            expendExpendedNode(expendedNode);
        }
        var nodeSelected = getPageSessionStorage(_this.ID + "NodeSelected");
        if (nodeSelected && nodeSelected != null) {
            selectSelectedNode(nodeSelected);
        }
        $(window).unload(function () {
            removePageSessionStorageByKey(_this.ID + "Expended");
            removePageSessionStorageByKey(_this.ID + "NodeSelected");
            window.status = "";
        });

        if ($("#" + _this.ID).width() < MaxWidth)
            $("#" + _this.ID).width(MaxWidth);

        $(".plus_" + _this.ID).click(function () {
            PlusClick(this);
        });

        $(".cb_" + _this.ID).click(function () {
            CheckBoxClick(this);
        });

        $(".treeNode_text").click(function () {
            if (_this.TextClickType == "expand") {
                PlusClick(this);
            }
            if (_this.TextClickFun != null && typeof (_this.TextClickFun) != "undefined") {
                _this.TextClickFun($(this).parent("li"));
            }

            $(".treeNodeSelected").removeClass("treeNodeSelected");
            var $this = $(this);
            setPageSessionStorage(_this.ID + "NodeSelected", $this.parent().attr("id"));
            $this.addClass("treeNodeSelected");
            var dataOnclick = $this.attr("data-onclick");
            window.status = $this.text();
            if (dataOnclick) {
                eval(dataOnclick);
            }
            return false;
        });   

        $(".treeNode[expand='false']").children(".plus_" + _this.ID).removeClass("treeNode_minus").addClass("treeNode_plus");
        $(".treeNode[expand='false']").children("ul").hide();       
    }

    var selectSelectedNode = function (selectedid) {
        var node = $("#" + selectedid);
        var iCount = node.length;
        if (iCount == 0) {
            return;
        } else if (iCount == 1) {
            node.children(".treeNode_text").addClass("treeNodeSelected");
        } else {
            selectSelectedNode(selectedid.substring(0, selectedid.length - 2));
        }
    }

    var expendExpendedNode = function (expendedid) {
        var node = $("#plus_" + expendedid);
        if (node.hasClass("treeNode_plus")) {
            var ul = $("#" + expendedid).children("ul");
            node.addClass("treeNode_minus").removeClass("treeNode_plus");
            ul.show();
        }
        if (expendedid.split('_').length > 2) {
            expendExpendedNode(expendedid.substring(0, expendedid.length - 2));
        }
    }
    _this.getSelectNode = function () {

        checkList = ""; //初始化已选项列表
        var firstNodeID = _this.ID + "_0";
        //判断根节点是否被选中
        if ($("#cb_" + firstNodeID).prop("checked") == true) {
            if (_this.ReturnType == 'value') {
                checkList += $("#" + firstNodeID).attr("nodevalue") + ",";
            }
            else if (_this.ReturnType == 'text') {
                checkList += $("#" + firstNodeID).attr("nodetext") + ",";
            }
            else if (_this.ReturnType == 'text:value') {
                checkList += $("#" + firstNodeID).attr("nodetext") + ":" + $("#" + firstNodeID).attr("nodevalue") + ",";
            }
        }

        GetSelectChild($("#" + firstNodeID));
        return checkList;
    }

    var InitTreeParent = function (itemList, parentID) {
        _this.Content = "<ul ";       
        if (parentID != "")
            _this.Content += "style='margin-left:16px;'>";

        if (itemList.ChildNodes.length > 0) {
            for (var ii = 0; ii < itemList.ChildNodes.length; ii++) {
                var childID = parentID + "_" + ii;
                if (parentID == "")
                    childID = _this.ID + "_" + ii;

                var childnode = itemList.ChildNodes[ii];
                //-----------------------
                //预计本结点的长度
                var vNodeWidth = (15 * childnode.Text.length) + (16 * childID.split('_').length);
                if (vNodeWidth > MaxWidth)
                    MaxWidth = vNodeWidth;

                //-----------------------
                var title = _this.SetNodeTextAsNodeTitle ? childnode.Text : childnode.Title;
                var vNode = "<li class='treeNode' id='" + childID + "' title='" + title + "' nodetext='" + childnode.Text + "' nodevalue='" + childnode.Value + "' nodeobj='" + childnode.Obj + "' expand='" + childnode.Expanded + "'>";

                var vPlus = "<span id='plus_" + childID+"' ";
                if (childnode.ChildNodes.length > 0) {
                  vPlus += "class='treeNode_minus plus_" + _this.ID + "'";
                }
                else {
                    vPlus += "class='treeNode_blank'";
                }
                vPlus += "/>";
                vNode += vPlus;

                //------------------------
                if (childnode.ShowCheckBox) {
                     var vChk = "<input type='checkbox' style='margin-right:4px;vertical-align:bottom;' id='cb_" + childID + "' class='cb_" + _this.ID+"'";
                    if (childnode.Checked) {
                        vChk += " checked='checked'";
                    }
                    var vChkSpan = "<span style='display:table-cell;vertical-align:bottom;'>"+vChk+"/></span>";                   
                    vNode += vChkSpan;
                }

                //------------------------
                if (childnode.ImageUrl != null) {
                    var vImg = "<img style='margin-right:4px;' src='" + getRootPath() + childnode.ImageUrl + "' width='" + _this.ImgWidth + "' height='" + _this.ImgHeight + "'/>";
                    var vImgSpan = "<span style='display:table-cell;vertical-align:bottom;'>" + vImg + "</span>";                   
                    vNode += vImgSpan;
                }

                //------------------------
                var vText = "<span class='treeNode_text'";
                if (_this.TextClickType == "expand") {
                    vText += " style='cursor:pointer;'";
                }
                else if (_this.TextClickType == "click") {
                    vText += " style='cursor:default;'";
                }

                var textHtml = childnode.TextHtml;
                if (!textHtml)
                    textHtml = childnode.Text;

                vText += ">" + textHtml + "</span>";
                vNode += vText;

                if (childnode.ClientFunName != null && childnode.ClientFunName != "") {
                    if (childnode.ClientFunName.indexOf("(") > -1) {
                        $(vText).attr("data-onclick", childnode.ClientFunName);
                    } else {
                        $(vText).attr("onclick", childnode.ClientFunName + "(this);")
                    }
                }               
                _this.Content += vNode;
                createChildTree(childnode, childID);
                _this.Content += "</li>";
            }
        }

        _this.Content += "</ul>";

        if (parentID == "") {
            $("#" + _this.ID).append(_this.Content);
        }
        else {
            $("#" + parentID).append(_this.Content);
        }

        _this.Content = '';
    }

    function createChildTree(itemList, parentID) {
        _this.Content += "<ul ";
        if (parentID != "")
            _this.Content += "style='margin-left:16px;'> ";        

        if (itemList.ChildNodes.length > 0) {
            for (var ii = 0; ii < itemList.ChildNodes.length; ii++) {
                var childID = parentID + "_" + ii;
                if (parentID == "")
                    childID = _this.ID + "_" + ii;

                var childnode = itemList.ChildNodes[ii];
                //-----------------------
                //预计本结点的长度
                var vNodeWidth = (15 * childnode.Text.length) + (16 * childID.split('_').length);
                if (vNodeWidth > MaxWidth)
                    MaxWidth = vNodeWidth;

                //-----------------------
                var title = _this.SetNodeTextAsNodeTitle ? childnode.Text : childnode.Title;
                var vNode = "<li class='treeNode' id='" + childID + "' title='" + title + "' nodetext='" + childnode.Text + "' nodevalue='" + childnode.Value + "' nodeobj='" + childnode.Obj + "' expand='" + childnode.Expanded + "'>";

                var vPlus = "<span id='plus_" + childID + "' ";
                if (childnode.ChildNodes.length > 0) {
                    vPlus += "class='treeNode_minus plus_" + _this.ID + "'";
                }
                else {
                    vPlus += "class='treeNode_blank'";
                }
                vPlus += "/>";
                vNode += vPlus;

                //------------------------
                if (childnode.ShowCheckBox) {
                    var vChk = "<input type='checkbox' style='margin-right:4px;vertical-align:bottom;' id='cb_" + childID + "' class='cb_" + _this.ID + "'";
                    if (childnode.Checked) {
                        vChk += " checked='checked'";
                    }
                    var vChkSpan = "<span style='display:table-cell;vertical-align:bottom;'>" + vChk + "/></span>";
                    vNode += vChkSpan;
                }

                //------------------------
                if (childnode.ImageUrl != null) {
                    var vImg = "<img style='margin-right:4px;' src='" + getRootPath() + childnode.ImageUrl + "' width='" + _this.ImgWidth + "' height='" + _this.ImgHeight + "'/>";
                    var vImgSpan = "<span style='display:table-cell;vertical-align:bottom;'>" + vImg + "</span>";
                    vNode += vImgSpan;
                }

                //------------------------
                var vText = "<span class='treeNode_text'";
                if (_this.TextClickType == "expand") {
                    vText += " style='cursor:pointer;'";
                }
                else if (_this.TextClickType == "click") {
                    vText += " style='cursor:default;'";
                }

                vText += ">" + childnode.Text + "</span>";
                vNode += vText;

                if (childnode.ClientFunName != null && childnode.ClientFunName != "") {
                    if (childnode.ClientFunName.indexOf("(") > -1) {
                        $(vText).attr("data-onclick", childnode.ClientFunName);
                    } else {
                        $(vText).attr("onclick", childnode.ClientFunName + "(this);")
                    }
                }

                _this.Content += vNode;
                createChildTree(childnode, childID);
                _this.Content += "</li>";
            }
        }

        _this.Content += "</ul>";
    }

    var PlusClick = function (vObj) {
        var vLi = $(vObj).parent("li");
        var vUl = $(vLi).children("ul");
        var vID = $(vLi).attr("id");
        var vPlus = $(vLi).children("#plus_" + vID);
        $(vUl).toggle();
        if ($(vUl).css("display") == "none") {
            $(vPlus).addClass("treeNode_plus");
            $(vPlus).removeClass("treeNode_minus");
        }
        else {
            $(vPlus).removeClass("treeNode_plus");
            $(vPlus).addClass("treeNode_minus");
            setPageSessionStorage(_this.ID + "Expended", vID);
        }
    }  

    var CheckBoxClick = function (sel) {
        if (_this.SelectAllChild) {
            var vID = $(sel).attr("id").replace('cb_', '')
            var vLi = $("#" + vID);
            var vUl = $(vLi).children("ul");

            if ($(sel).prop("checked")) {
                $(vUl).find(":checkbox").prop("checked", "true");
            }
            else {
                $(vUl).find(":checkbox").removeAttr("checked");
            }
        }
    }

    var GetSelectChild = function (vLi) {
        var col = $(vLi).children("ul").children("li");
        for (var ii = 0; ii < col.length; ii++) {
            var vID = $(col[ii]).attr("id");
            if ($("#cb_" + vID).prop("checked") == true) {
                if (_this.ReturnType == 'value') {
                    checkList += $("#" + vID).attr("nodevalue") + ",";
                }
                else if (_this.ReturnType == 'text') {
                    checkList += $("#" + vID).attr("nodetext") + ",";
                }
                else if (_this.ReturnType == 'text:value') {
                    checkList += $("#" + vID).attr("nodetext") + ":" + $("#" + vID).attr("nodevalue") + ",";
                }
            }

            if ($("#" + vID).children("ul").length != 0)
                GetSelectChild($("#" + vID));
        }
    }
}